/**
 * desc: 公共接口
 */

import request from '@/static/uitls/request.js'

//订阅消息
const getTemplateList = ()=>{
  return request({
    url: '/api/v1/sms/templateList',
    method: 'GET'
  })
}

//客服电话
const getPhone = ()=>{
  return request({
    url: '/api/v1/setting/service',
    method: 'GET'
  })
}

//阿里云OSS
const ossSign = ()=>{
  return request({
    url: '/teacher/v1/oss/sign',
    method: 'GET'
  })
}

// 手机号解密
const decrypt = data=>{
  return request({
    url: '/teacher/v1/user/decrypt',
    method: 'POST',
	data
  })
}

// 获取待评价的id
const getToBeEvaluated = ()=>{
  return request({
    url: '/api/v1/user.order/comment',
    method: 'GET'
  })
}

// 地址相关接口  star
//获取全国城市数据
const getCityList = ()=>{
  return request({
    url: '/api/v1/address/city',
    method: 'GET'
  })
}

//获取全国省市区数据
const getAreaList = ()=>{
  return request({
    url: '/api/v1/address/area',
    method: 'GET'
  })
}

//获取用户地址列表
const getAddressList = (showOnload=true)=>{
  return request({
    url: '/api/v1/address',
    method: 'GET',
	showOnload
  })
}

//新增用户地址
const addAddress = data=>{
  return request({
    url: '/api/v1/address/create',
    method: 'POST',
	data
  })
}

//用户地址详情
const detailAddress = id=>{
  return request({
    url: '/api/v1/address/read?address_id='+id,
    method: 'GET'
  })
}
//删除用户地址
const delAddress = id=>{
  return request({
    url: '/api/v1/address/delete?address_id='+id,
    method: 'GET'
  })
}
//更改用户地址
const updAddress = (id,data)=>{
  return request({
    url: '/api/v1/address/update?address_id='+id,
    method: 'POST',
	data
  })
}
//用户信息
const getMineInfo = ()=>{
  return request({
    url: '/api/v1/user/info',
    method: 'GET',
	showOnload:false
  })
}
//关于可语
const getAbout = ()=>{
  return request({
    url: '/api/v1/setting/about',
    method: 'GET'
  })
}

//生成小程序码
const getPageCode = (scene,page)=>{
  return request({
    url: '/api/v1/setting/code',
    method: 'POST',
	data:{
		scene:scene,
		page:page
	}
  })
}

//学生我的课件
const studentCourseWare = (page,size)=>{
  return request({
    url: '/api/v1/user.file/index',
    method: 'POST',
	data:{
		page:page,
		size:size
	}
  })
}


//绑定手机号
const bind = (data)=>{
  return request({
    url: '/api/v1/user/bind',
    method: 'POST',
	data
  })
}


// 经纬度逆地址解析
const inverseResolution = (address,city='') => {  //导航用到
	const app = getApp();
	return request({
		url: 'https://apis.map.qq.com/ws/geocoder/v1/',
		method: 'GET',
		data:{
			address:address,
			key:app.globalData.addressKey,
			city:city
		}
	});
}

// 经纬度逆地址解析
const reverseAddressResolution = (lat,lon,poi=1) => {
	const app = getApp();
	return new Promise((resolve, reject) => {
		request({
			url: 'https://apis.map.qq.com/ws/geocoder/v1/',
			method: 'GET',
			data:{
				location:lat+','+lon,
				key:app.globalData.addressKey,
				get_poi:poi
			}
		}).then(res=>{
			if(res.status==0){ // 请求成功时预处理位置信息
				// let {lat,lng}=app.globalData.location;
				if(Number(app.globalData.location.lat)!=0){ // 全局的经纬度一致的话更新城市和就近地址字符串
					app._log('预处理',res)
					let {address_component,formatted_addresses,location}=res.result;
					app.globalData.location.lat = location.lat;
					app.globalData.location.lng = location.lng;
					app.globalData.location.city = address_component.city;
					app.globalData.location.address = formatted_addresses.recommend;
				}
			}
			resolve(res)
		})
	});
}


/**
 * nearAddressInformation 根据地址信息或经纬度获取附近数据
 * @method keyword  搜索关键词
 * @method region  搜索城市 （地址信息搜索，如果是经纬度搜索，取值是经度）
 */

// 传参有以下2种情况
//keyword  region  page_index  page_size
//keyword  lat  lon  page_index  page_size
const nearAddressInformation = (...d) => {
	let a = [...d]; //传参
	let boundary = ''; //搜索方式
	let page_index = 1; //搜索页码
	let page_size = 10; // 搜索长度
	if(a.length==4){ // 地址信息搜索
		boundary = 'region('+a[1]+',0)';
		page_index = a[2];
		page_size = a[3];
	}else{ // 经纬度搜索
		boundary = `nearby(${a[1]},${a[2]},1000)`;
		page_index = a[3];
		page_size = a[4];
	}
	const app = getApp();
	app._log(boundary);
  return request({
    url: 'https://apis.map.qq.com/ws/place/v1/search',
    method: 'GET',
    data:{
		keyword:a[0], 
		boundary:boundary,
		key:app.globalData.addressKey,
		page_index:page_index,
		page_size:page_size
	}
  })
}


// 地址相关接口  end


module.exports = {
	getTemplateList,
	getPhone,
	ossSign,
	decrypt,
	getToBeEvaluated,
	getCityList,
	getAreaList,
	getAddressList,
	addAddress,
	delAddress,
	updAddress,
	detailAddress,
	getMineInfo,
	getAbout,
	getPageCode,
	inverseResolution,
	reverseAddressResolution,
	nearAddressInformation,
	studentCourseWare,
	bind
}